<template>
  <div class="app-container">
    <el-dialog :visible.sync="show" :close-on-click-modal="false" width="80%">
      <div slot="title" class="dialog-header">
        <span>关联检查项 - {{ currentRow.businessType }}</span>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="addChkitem" class="operate-add-btn" v-if="showBtns.add">新增</el-button>
      </div>
      <el-table v-loading="listLoading" :data="list" size="small" height="calc(100vh - 200px)">
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="检查项编码" min-width="80">
          <span slot-scope="{ row }">
            {{ row.chkitemCode }}
          </span>
        </el-table-column>
        <el-table-column label="扩展类型" align="center" min-width="100">
          <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_business_type_expand" :value="row.businessTypeExpand" />
        </el-table-column>
        <el-table-column label="检查项名称" align="center" min-width="80">
          <span slot-scope="{ row }">{{ row.chkitemName }}</span>
        </el-table-column>
        <!-- <el-table-column label="检查类型" align="center" min-width="80">
          <template slot-scope="{ row }">{{ row.businessType }}</template>
        </el-table-column>-->
        <el-table-column label="检查维度" prop="dimension" min-width="80">
          <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_dimension" :value="row.dimension" />
        </el-table-column>
        <el-table-column label="检查方式" prop="generationType" min-width="80">
          <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_generation_type" :value="row.generationType" />
        </el-table-column>
        <!--<el-table-column label="检查版本" align="center"  min-width="120">
          <span slot-scope="{ row }">{{ row.chkitemVersion }}</span>
        </el-table-column>-->
        <el-table-column label="进入条件" align="center" min-width="160">
          <span slot-scope="{ row }">{{ row.entryConditionName }}</span>
        </el-table-column>
        <el-table-column label="退出条件" align="center" min-width="160">
          <span slot-scope="{ row }">{{ row.exitConditionName }}</span>
        </el-table-column>
        <el-table-column label="频次" align="center" min-width="60">
          <template slot-scope="{ row }">
            <span>{{ row.frequency }}</span>
            <span>次</span>
            <span v-if="row.frequencyUnit && row.frequencyUnit !== 'EMPTY'">/{{ row.frequencyUnit | dictFilter(dict.type.cq_pe_frequency_unit) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属系统" prop="belongSystem" min-width="80">
          <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_belong_system" :value="row.belongSystem" />
        </el-table-column>
        <el-table-column label="修改人" prop="updatorName" align="center" min-width="60" />
        <el-table-column label="修改时间" prop="updateTime" align="center" min-width="120">
          <span slot-scope="{ row }">{{ row.updateTime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="120">
          <template slot-scope="{ row }">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditItem.open(row)" v-if="showBtns.edit">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-if="showBtns.remove">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <EditItem ref="EditItem" :list="list" v-model="showEditItem" @ok="getList" />
  </div>
</template>
<script>
// 工程质量-检查项
import { listBsChkitem, delBsChkitem } from '@/api/cqPe/bsChkitem'
import EditItem from './components/EditBsChkitem'
import auth from '@/plugins/auth'
export default {
  name: 'BsChkitem',
  components: { EditItem },
  props: {
    rdcType: {
      type: String,
      default: () => ''
    }
  },
  data() {
    return {
      show: false,
      currentRow: {},
      listLoading: false,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 1000,
        bsNodeId: ''
      },
      showEditItem: false
    }
  },
  computed: {
    showBtns() {
      // 操作按钮权限
      const deptKey = this.rdcType.toLowerCase()
      const edit = auth.hasPermi(`cqPe:bsNode:${deptKey}:relation:edit`)
      const add = auth.hasPermi(`cqPe:bsNode:${deptKey}:relation:add`)
      const remove = auth.hasPermi(`cqPe:bsNode:${deptKey}:relation:delete`)
      return { edit, add, remove }
    }
  },
  dicts: ['cq_random_type', 'cq_pe_frequency_unit', 'cq_pe_business_type_expand', 'cq_pe_belong_system', 'cq_pe_generation_type', 'cq_pe_dimension'],
  created() {
    // this.getList()
  },
  methods: {
    open(row) {
      this.currentRow = row
      this.queryParams.bsNodeId = row.bsNodeId
      this.show = true
      this.getList()
    },
    addChkitem() {
      this.showEditItem = true
      for (const key in this.$refs.EditItem.form) {
        if (this.currentRow[key]) {
          this.$refs.EditItem.form[key] = this.currentRow[key]
        }
      }
      // this.$refs.EditItem.form.bsNodeId = this.currentRow.bsNodeId
      // this.$refs.EditItem.form.businessType = this.currentRow.businessType
      // this.$refs.EditItem.form.belongSystem = this.currentRow.belongSystem
      // this.$refs.EditItem.form.entryCondition = this.currentRow.entryCondition
      // this.$refs.EditItem.form.exitCondition = this.currentRow.exitCondition
      // this.$refs.EditItem.form.frequency = this.currentRow.frequency
      // this.$refs.EditItem.form.frequencyUnit = this.currentRow.frequencyUnit
      // this.$refs.EditItem.form.dimension = this.currentRow.dimension
      // this.$refs.EditItem.form.generationType = this.currentRow.generationType
    },
    async getList() {
      this.listLoading = true
      const res = await listBsChkitem(this.queryParams).catch(() => false)
      this.listLoading = false
      if (res === false) return
      const data = res.rows || []
      data.map(i => {
        i.removeLoading = false
        i.isEnableLoading = false
      })
      this.list = data
      this.total = res.total || 0
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除：${item.chkitemName}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delBsChkitem(item.bsChkitemId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    }
  }
}
</script>
<style scoped lang="scss">
  .dialog-header {
    .operate-add-btn {
      margin-left: 100px;
    }
  }
</style>
